<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="性别" prop="sex">
                <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small" style="width: 240px">
                    <el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                </el-select>
            </el-form-item>

            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择运动员状态" clearable size="small" style="width: 240px">
                    <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['training:sportsman:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['training:sportsman:remove']">删除
                </el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="sportsmanList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!--            <el-table-column label="PK" align="center" prop="id"/>-->
            <el-table-column label="姓名" align="center" prop="name" />
            <el-table-column label="性别" :formatter="sexFormat" align="center" prop="sex" />
            <el-table-column label="梯队" :formatter="echelonFormat" align="center" prop="echelon" />
            <el-table-column label="出生日期" align="center" prop="birthday" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="身高" align="center" prop="height" />
             <el-table-column label="体重" align="center" prop="weight" />
             <el-table-column label="坐高" align="center" prop="sitHigh" />
             <el-table-column label="臂展" align="center" prop="armSpread" />
             <el-table-column label="肩宽" align="center" prop="shoulderWidth" />
             <el-table-column label="腿长" align="center" prop="legLength" />
             <el-table-column label="小腿长" align="center" prop="tinyLegLength" />
             <el-table-column label="胸围" align="center" prop="bust" />
             <el-table-column label="臀围" align="center" prop="hipline" />
             <el-table-column label="腰围" align="center" prop="waistline" />-->
            <el-table-column label="参训时间" align="center" prop="trainingTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.trainingTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="技术等级" align="center" prop="level" />
            <!--      <el-table-column label="技术等级授予单位" align="center" prop="ratingUnit" />-->
            <el-table-column label="输送单位" align="center" prop="conveyingUnit" />
            <el-table-column label="证书编号" align="center" prop="certificateNumber" />
            <el-table-column label="创建时间" align="center" prop="ctime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.ctime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <!--      <el-table-column label="更新时间" align="center" prop="ltime" width="180">-->
            <!--        <template slot-scope="scope">-->
            <!--          <span>{{ parseTime(scope.row.ltime, '{y}-{m}-{d}') }}</span>-->
            <!--        </template>-->
            <!--      </el-table-column>-->
            <!--      <el-table-column label="部门ID" align="center" prop="deptid" />-->
            <!--      <el-table-column label="头像地址" align="center" prop="imgUrl" />-->
            <el-table-column label="状态" :formatter="statusFormat" align="center" prop="status" />
            <!--      <el-table-column label="学历" align="center" prop="education" />-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['training:sportsman:edit']">修改
                    </el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['training:sportsman:remove']">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改sportsman对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="90px">
                <el-col :span="12">
                    <el-form-item label="姓名" prop="name">
                        <el-input v-model="form.name" placeholder="请输入姓名" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="性别" :formatter="sexFormat" prop="sex">
                        <el-select v-model="form.sex" placeholder="请选择性别" clearable size="small" style="width: 240px">
                            <el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="出生日期" prop="birthday">
                        <el-date-picker clearable size="small" style="width: 200px" v-model="form.birthday" type="date" value-format="yyyy-MM-dd" placeholder="选择出生日期">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="参训时间" prop="trainingTime">
                        <el-date-picker clearable size="small" style="width: 200px" v-model="form.trainingTime" type="date" value-format="yyyy-MM-dd" placeholder="选择参训时间">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="梯队" prop="echelon">
                            <el-select v-model="form.echelon" placeholder="请选择梯队" clearable size="small" style="width: 240px;">
                                <el-option v-for="dict in echelonList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="身高" prop="height">
                            <el-input v-model="form.height" placeholder="请输入身高" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-col :span="12">
                    <el-form-item label="坐高" prop="sitHigh">
                        <el-input v-model="form.sitHigh" placeholder="请输入坐高" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="臂展" prop="armSpread">
                        <el-input v-model="form.armSpread" placeholder="请输入臂展" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="肩宽" prop="shoulderWidth">
                        <el-input v-model="form.shoulderWidth" placeholder="请输入肩宽" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="腿长" prop="legLength">
                        <el-input v-model="form.legLength" placeholder="请输入腿长" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="小腿长" prop="tinyLegLength">
                        <el-input v-model="form.tinyLegLength" placeholder="请输入小腿长" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="胸围" prop="bust">
                        <el-input v-model="form.bust" placeholder="请输入胸围" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="臀围" prop="hipline">
                        <el-input v-model="form.hipline" placeholder="请输入臀围" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="腰围" prop="waistline">
                        <el-input v-model="form.waistline" placeholder="请输入腰围" />
                    </el-form-item>
                </el-col>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="技术等级" prop="level">
                            <el-select v-model="form.level" placeholder="请选择技术等级" clearable size="small" style="width: 240px">
                                <el-option v-for="dict in technicalGrade" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictLabel" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="证书编号" prop="certificateNumber">
                            <el-input v-model="form.certificateNumber" placeholder="请输入证书编号" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-col :span="12">
                    <el-form-item label="授予单位" prop="ratingUnit">
                        <el-input v-model="form.ratingUnit" placeholder="请输入技术等级授予单位" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="输送单位" prop="conveyingUnit">
                        <el-input v-model="form.conveyingUnit" placeholder="请输入输送单位" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="部门ID" prop="deptid">
                        <el-input v-model="form.deptid" placeholder="请输入部门ID" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="头像地址" prop="imgUrl">
                        <el-input v-model="form.imgUrl" placeholder="请输入头像地址" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="状态" :formatter="statusFormat" prop="status">
                        <el-select v-model="form.status" placeholder="请选择运动员状态" clearable size="small" style="width: 240px">
                            <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="学历" prop="education">
                        <el-select v-model="form.education" placeholder="请选择学历" clearable size="small" style="width: 240px">
                            <el-option v-for="dict in educationOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictLabel" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="登录账号" prop="accountNumber">
                        <el-input v-model="form.accountNumber" placeholder="请输入账号" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="登录密码" prop="password">
                        <el-input v-model="form.password" placeholder="请输入密码" />
                    </el-form-item>
                </el-col>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listSportsman,
    getSportsman,
    delSportsman,
    addSportsman,
    updateSportsman,
    exportSportsman
} from "@/api/training/sportsman";

export default {
    name: "Sportsman",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // sportsman表格数据
            sportsmanList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            //性别字典
            sexOptions: [],
            //状态字典
            statusOptions: [],
            //学历字典
            educationOptions: [],
            //技术等级
            technicalGrade: [],
            //梯队字典
            echelonList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: undefined,
                sex: undefined,
                birthday: undefined,
                height: undefined,
                weight: undefined,
                sitHigh: undefined,
                armSpread: undefined,
                shoulderWidth: undefined,
                legLength: undefined,
                tinyLegLength: undefined,
                bust: undefined,
                hipline: undefined,
                waistline: undefined,
                trainingTime: undefined,
                level: undefined,
                ratingUnit: undefined,
                conveyingUnit: undefined,
                ctime: undefined,
                ltime: undefined,
                deptid: undefined,
                imgUrl: undefined,
                status: undefined,
                education: undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "姓名不能为空", trigger: "blur" }
                ],
                sex: [
                    { required: true, message: "性别不能为空", trigger: "blur" }
                ],
                trainingTime: [
                    { required: true, message: "参训时间不能为空", trigger: "blur" }
                ],
                birthday: [
                    { required: true, message: "出生日期不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();

        this.getDicts("training_sex").then(response => {
            this.sexOptions = response.data;
        });

        this.getDicts("training_status_6").then(response => {
            this.statusOptions = response.data;
        });

        this.getDicts("education").then(response => {
            this.educationOptions = response.data;
        });

        this.getDicts("technical_grade").then(response => {
            this.technicalGrade = response.data;
        });

        this.getDicts("echelon").then(response => {
            this.echelonList = response.data;
        });

    },
    methods: {
        /** 查询sportsman列表 */
        getList() {
            this.loading = true;
            listSportsman(this.queryParams).then(response => {
                this.sportsmanList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },

        // 性别字典翻译
        sexFormat(row, column) {
            return this.selectDictLabel(this.sexOptions, row.sex);
        },

        // 梯队字典翻译
        echelonFormat(row, column) {
            return this.selectDictLabel(this.echelonList, row.echelon);
        },

        // 梯队字典翻译
        statusFormat(row, column) {
            return this.selectDictLabel(this.statusOptions, row.status);
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                name: undefined,
                sex: undefined,
                birthday: undefined,
                height: undefined,
                weight: undefined,
                sitHigh: undefined,
                armSpread: undefined,
                shoulderWidth: undefined,
                legLength: undefined,
                tinyLegLength: undefined,
                bust: undefined,
                hipline: undefined,
                waistline: undefined,
                trainingTime: undefined,
                level: undefined,
                ratingUnit: undefined,
                conveyingUnit: undefined,
                ctime: undefined,
                ltime: undefined,
                deptid: undefined,
                imgUrl: undefined,
                status: "0",
                education: undefined,
                accountNumber: undefined,
                password: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加sportsman";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getSportsman(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改运动员基本信息";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateSportsman(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        addSportsman(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除sportsman编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delSportsman(ids);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(function () {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有sportsman数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return exportSportsman(queryParams);
            }).then(response => {
                this.download(response.msg);
            }).catch(function () {
            });
        }
    }
};
</script>